import { ValueT } from '@/type';
import { Color } from '@/utils/CanvasUtils/CanvasType';
import { WatchViewI } from '../type';
import CanvasUtils from '@/utils/CanvasUtils';

/** 绘制矩形参数 */
export interface RectStyle {
	/** x位置 */
	x: ValueT<number>;
	/** y位置 */
	y: ValueT<number>;
	/** y位置 */
	width: ValueT<number>;
	/** y位置 */
	height: ValueT<number>;
	/** 圆角半径 */
	radius?: ValueT<number>;
	/** 填充颜色 */
	color?: ValueT<Color>;
	/** 边框颜色 */
	borderColor?: ValueT<Color>;
	/** 线条宽度 */
	lineWidth?: ValueT<number>;
	/** 透明度 */
	transparency?: ValueT<number>;
	/** 是否显示 */
	show?: ValueT<boolean>;
}

export default class RectView implements WatchViewI {

	public Z_Index: ValueT<number>;

	/** 画板类 */
	public canvas: CanvasUtils;

	/** 绘制矩形参数 */
	public save: RectStyle;

	/**
	 * 构造函数
	 * @param canvas 画板类
	 * @param save 绘制矩形参数
	 * @param shwo 是否显示
	 * @param zIndex 绘制级别(最大最上)
	 */
	constructor(canvas: CanvasUtils, save: RectStyle, zIndex?: ValueT<number>) {
		this.canvas = canvas;
		this.save = save;
		this.Z_Index = zIndex || { Value: 0 };
	}

	/** @override */
	public Draw() {
		const { canvas, save } = this;
		const { x, y, width, height, radius, color, borderColor, lineWidth, transparency, show } = save;
		if (show && !show.Value) { return; }
		const widthV = width.Value;
		const heightV = height.Value;
		canvas.Save();
		if (transparency) {
			const transparencyV = transparency.Value;
			canvas.GlobalAlpha = transparencyV;
		}
		if (!color && !borderColor) { return; }
		canvas.Translate(x.Value, y.Value);
		if (radius) {
			const colorV = !color ? undefined : color.Value;
			const borderColorV = !borderColor ? undefined : borderColor.Value;
			if (colorV) { canvas.FillStyle = colorV; }
			if (borderColorV) {
				if (lineWidth) { canvas.LineWidth = lineWidth.Value; }
				canvas.StrokeStyle = borderColorV;
			}
			canvas.RoundRect({ Rect: { Spot: { X: 0, Y: 0 }, Width: widthV, Height: heightV }, Radius: radius.Value, BackgroundColor: colorV, BorderColor: borderColorV })
		} else {
			if (color) {
				canvas.FillStyle = color.Value;
				canvas.FillRect({ Spot: { X: 0, Y: 0 }, Width: widthV, Height: heightV });
			}
			if (borderColor) {
				if (lineWidth) {
					canvas.LineWidth = lineWidth.Value;
				}
				canvas.StrokeStyle = borderColor.Value;
				canvas.StrokeRect({ Spot: { X: 0, Y: 0 }, Width: widthV, Height: heightV });
			}
		}
		canvas.Restore();
	}

}
